<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=
    , initial-scale=1.0">
    <title>demo</title>
    <link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon">
</head>
<body>
    <div id="test">

    </div>
    
    <script type="text/javascript" src="../js/react.development.js"></script>
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <script type="text/javascript" src="../js/babel.min.js"></script>

    <script type="text/babel">
        class Life extends React.Component {

            death = () => {
                // 取消挂载
                ReactDOM.unmountComponentAtNode(document.getElementById('test'))
            }

            state = {
                opacity: 1
            }

            // 组件挂载页面之后调用
            componentDidMount() {
                this.timmer = setInterval(() => {
                    this.setState({opacity: this.state.opacity - 0.1})
                    if (this.state.opacity < 0) this.setState({opacity: 1})
                }, 200);
            }

            // 组件卸载之前
            componentWillUnmount() {
                clearInterval(this.timmer)
            }

            // 初始化渲染、状态更新之后调用
            render() {
                return (
                    <div>
                        <h2 style={{opacity: this.state.opacity}}>React学不会怎么办？</h2>
                        <button onClick={this.death}>不活了</button>    
                    </div>
                )
            }
        }

        ReactDOM.render(<Life/>, document.getElementById("test"))
    </script>
</body>
</html>